<template>
  <BasicDialog ref="basicDialog" :isVisible="isAlert" width="30%" :title="title" @handleSure="handleSure" :isShowSure="flag != 'job_look'">
    <template #content>
      <el-form ref="formRef" :model="formInfo" :rules="flag != 'job_look' ? rules : {}" label-width="120px">
        <el-form-item label="任务名称:" prop="jobName">
          <el-input v-if="flag != 'job_look'" type="text" v-model.trim="formInfo.jobName" placeholder="任务名称"
            clearable></el-input>
          <span v-else>{{ formInfo.jobName }}</span>
        </el-form-item>
        <el-form-item label="任务组名:" prop="jobGroup">
          <el-input v-if="flag != 'job_look'" type="text" v-model.trim="formInfo.jobGroup"
            placeholder="任务组名" clearable></el-input>
          <span v-else>{{ formInfo.jobGroup }}</span>
        </el-form-item>
        <el-form-item label="任务类全名:" prop="jobClassName">
          <el-text v-if="flag != 'job_look'" size="small">任务类全名（包含包名）且类需继承AbstractQuartzJob</el-text>
          <el-input v-if="flag != 'job_look'" type="text" maxlength="255" v-model.trim="formInfo.jobClassName"
            placeholder="任务类全名" clearable></el-input>
          <span v-else>{{ formInfo.jobClassName }}</span>
        </el-form-item>
        <el-form-item label="参数:" prop="parameter">
          <el-input v-if="flag != 'job_look'" type="text" maxlength="500" v-model.trim="formInfo.parameter"
            placeholder="参数" clearable></el-input>
          <span v-else>{{ formInfo.parameter }}</span>
        </el-form-item>
        <el-form-item label="cron表达式:" prop="cronExpression">
          <el-input v-if="flag != 'job_look'" type="text" maxlength="500" v-model.trim="formInfo.cronExpression"
            placeholder="cron执行表达式" clearable></el-input>
          <span v-else>{{ formInfo.cronExpression }}</span>
        </el-form-item>
        <el-form-item label="是否一次性:" prop="sysJobOnce">
          <el-select v-if="flag != 'job_look'" v-model="formInfo.sysJobOnce" placeholder="请选择" clearable
            style="width: 100%">
            <el-option :value="'1'" label="一次性任务" />
            <el-option :value="'0'" label="持续性任务" />
          </el-select>
          <span v-else>{{ formInfo.sysJobOnce === '1'? '一次性任务' : '持续性任务' }}</span>
        </el-form-item>
      </el-form>
    </template>
  </BasicDialog>
</template>
<script lang='ts' setup>
import { FormInfo } from "@components/common/BasicForm/BasicFormTypes";
import type { FormRules, FormInstance } from 'element-plus';
import { jobManageAdd, jobManageEdit, getJobManageById } from "@/request/apis/job/jobManage"
const props = defineProps({
  info: {
    type: Object,
    default: () => {
      return {}
    }
  },
  flag: {
    type: String,
    default: ''
  },
  isAlert: {
    type: Boolean,
    default: false
  },
})

const $emits = defineEmits([
  "closeDialog"
])

// 提交表单校验
const formRef = ref<FormInstance>();
const basicDialog = ref();

async function handleSure() {
  await formRef.value.validate();
  basicDialog.value.changeLoading(true);

  try{
    if(props.flag == 'job_add'){//新增
      await jobManageAdd(formInfo.value);
      $emits("closeDialog", 1);
      ElMessage.success("操作成功")
    }else if(props.flag == 'job_edit'){//修改
      await jobManageEdit(formInfo.value);
      $emits("closeDialog", 1);
      ElMessage.success("操作成功")
    }else{//查看
      basicDialog.value.changeLoading(false);
      $emits("closeDialog", 0);
    }
  }catch(error){
    basicDialog.value.changeLoading(false);
  }
}

//表单数据 
let formInfo = ref<FormInfo>({});
// 表单校验规则
const rules = {
  jobName: [{ required: true, message: "不能为空", trigger: "blur" }],
  jobGroup: [{ required: true, message: "不能为空", trigger: "blur" }],
  jobClassName: [{ required: true, message: "不能为空", trigger: "blur" }],
  cronExpression: [{ required: true, message: "不能为空", trigger: "blur" }],
  sysJobOnce: [{ required: true, message: "不能为空", trigger: "blur" }],
}
const title = ref("");
onMounted(async () => {
  if (props.flag == "job_add") {
    title.value = '新增'
  } else if (props.flag == "job_edit") {
    title.value = '修改'
    formInfo.value = await getJobManageById(props.info.jobId)
  } else if (props.flag == "job_look") {
    title.value = '查看'
    formInfo.value = await getJobManageById(props.info.jobId)
  }
})

</script>

<style scoped lang="scss"></style>